import React from 'react';
import {
  AsyncStorage,
} from 'react-native';

import Config from '../config';
import Constants from '../common/Constants';
import PostConstants from '../common/PostConstants';
import BaseModelNew from './BaseModelNew';

export default class WorkHardModel extends BaseModelNew {
  constructor() {
    super();

    this.moduleType = Constants.MODULE_TYPE.WORK_HARD;
    this.requestPosts = this.requestPosts.bind(this);
    this.requestPaihangbang = this.requestPaihangbang.bind(this);
    this.requesthuojiang = this.requesthuojiang.bind(this);
    this.requestOfficial = this.requestOfficial.bind(this);
    this.requestVotec = this.requestVotec.bind(this);
    this.requestDepartment = this.requestDepartment.bind(this);
    this.createPost = this.createPost.bind(this);
    this.createOfficialPost = this.createOfficialPost.bind(this);
    this.requestComment = this.requestComment.bind(this);
    this.requestContent = this.requestContent.bind(this);
    this.sendComment = this.sendComment.bind(this);
    this.sendSearch = this.sendSearch.bind(this);
    this.sendIdeasVote = this.sendIdeasVote.bind(this);
    this.saveData = {requestPosts:{}};
  }

  //请求创新创效帖子
  //sort_id 排序方式，1最新，2最热
  //tag 1金点子，2曝光台，3勤俭模范
  //page 第几页
  //page_size 每页几项
  //http://118.89.111.124:8080/dqapp/boards/' + PostConstants.BOARD_ID.WORK_HARD + '/posts?page=1&page_size=5&sort_id=1&tag=1
  requestPosts(actionData, callback) {
    var url = Config.serverNoHostUrl + '/ideas?page=' + actionData.page +
      '&page_size=' + actionData.page_size + '&sort_id=' + actionData.sort_id ;
    if(actionData.recommend) {
      url += "&recommend=" + actionData.recommend;
    }
    if(actionData.sub_tag) {
      url += "&sub_tag=" + actionData.sub_tag;
    }
    if(actionData.official ) {
      url += "&official=" + actionData.official ;
    }
    this.sendGet(url, callback);
  }
  requestPaihangbang(actionData, callback) {
    var url = Config.serverNoHostUrl + '/ideas/rank?page=' + actionData.page +
      '&page_size=' + actionData.page_size + '&type=' + actionData.type ;
    // if(actionData.year) {
      url += "&year=" + actionData.year;
    // }
    if(actionData.month) {
      url += "&month=" + actionData.month;
    }
    this.sendGet(url, callback);
  }
  requestVotec(actionData, callback) {
    var url = Config.serverNoHostUrl + '/ideas/vote?page=' + actionData.page +
      '&page_size=' + actionData.page_size + '&type=' + actionData.type ;
    this.sendGet(url, callback);
  }
  requestOfficial(actionData, callback) {
    var url = Config.serverNoHostUrl + '/ideas/' + actionData.post_id + '/comments/official'
    console.log(url)
    this.sendGet(url, callback);
  }
  //http://43.241.76.189:40000/ideas/department?page=1&page_size=10&official=0
  requestDepartment(actionData, callback) {
    var url = Config.serverNoHostUrl + '/ideas/department?page=' + actionData.page +
      '&page_size=' + actionData.page_size;
    if(actionData.official!==undefined) {
      url += "&official=" + actionData.official;
    }
    if(actionData.recommend!==undefined) {
      url += "&recommend=" + actionData.recommend;
    }
    this.sendGet(url, callback);
  }
  //http://127.0.0.1:40000/ideas/prize?page=1&tip_type=1&year=2018&tip=20&month=3
  requesthuojiang(actionData, callback) {
    var url = Config.serverNoHostUrl + '/ideas/prize?page=' + actionData.page +
      '&page_size=' + actionData.page_size;
      url += "&year=" + actionData.year;
      url += "&tip_type=" + actionData.tip_type;
      if (actionData.tip) {
        url += "&tip=" + actionData.tip;
      }
    if(actionData.month) {
      url += "&month=" + actionData.month;
    }
    this.sendGet(url, callback);
  }
  //发表帖子 CreatePostPayload
  createPost(data, callback) {
    var sendData = {
      "content": data.content,
      "tag": parseInt(data.tag),
      "sub_tag": parseInt(data.sub_tag),
      "title": data.title,
      "anonymous": data.anonymous,
       "image_urls": data.image_urls,
    }
    this.sendPost(Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.WORK_HARD + '/posts', sendData, callback);
  }
  //发表帖子 CreatePostPayload
  createOfficialPost(data, callback) {
    var sendData = {
      "recommend": data.recommend,
      "content": data.content,
      "official": data.official,
      "tip":data.tip||0,
    }
    this.sendPost(Config.serverNoHostUrl + '/ideas/' + data.post_id + '/comments/official', sendData, callback);
  }
  sendIdeasVote(data, callback) {
    var sendData = {
      type:data.type||1,
      vote:data.vote,
    }
    this.sendPost(Config.serverNoHostUrl + '/ideas/vote/' + data.post_id , sendData, callback);
  }
  //board_id 版块ID, 目前只能填1，创新创效
  //page
  //page_size
  //post_id 帖子ID
  //http://118.89.111.124:8080/dqapp/boards/' + PostConstants.BOARD_ID.WORK_HARD + '/posts/1111/comments?page=1&page_size=5
  requestComment(data, callback) {
    var board_id = 1;
    if(data.board_id){
      board_id =data.board_id;
    }
    this.sendGet(Config.serverNoHostUrl + '/ideas/' + data.post_id + "/comments?page=" + data.page + "&page_size=" + data.page_size,
      callback);
  }

  //请求内容
  requestContent(data, callback) {
    this.sendGet(Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.WORK_HARD + '/posts/' + data.post_id,
      callback);
  }

  //发表评论  POST /ideas/{post_id}/comments
  //http://118.89.111.124:8080/dqapp/boards/' + PostConstants.BOARD_ID.WORK_HARD + '/posts/11111/comments
  sendComment(data, callback) {
    var sendData = {
      "content": data.comment,
    }
    var board_id = 1;
    if(data.board_id){
      board_id =data.board_id;
    }

    this.sendPost(Config.serverNoHostUrl + '/ideas/' + data.post_id + '/comments', sendData, callback);
  }

  //搜索http://118.89.111.124:8080/dqapp/boards/' + PostConstants.BOARD_ID.WORK_HARD + '/posts/searched?key=1&page=1&page_size=5&sub_tag=1&tag=1
  // sub_tag (integer, optional): 子标签，从1开始，分别为原油开发、天然气开发、钻探工程、工程建设、机械制造、生产保障、石油化工、企业管理、安全环保、质量节能、科技创新、市场开发、人才培养、企业文化、党建工作、信息技术、矿区服务
  sendSearch(data, callback) {
    this.sendGet(Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.WORK_HARD + '/posts/searched?key=' + data.searchText + "&page=" + data.page + "&page_size=10&sub_tag=" + data.sub_tag + "&tag=" + data.tag,
      callback);
  }
}
